package com.jxc.dao.impl;

import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.jxc.dao.OrderDao;
import com.jxc.entity.Order;
import com.jxc.util.PageBean;

public class OrderDaoImpl extends HibernateDaoSupport implements OrderDao {

	public void delete(Order order) {

		getHibernateTemplate().delete(order);
	}

	public Order getOrder(Order order) {
       Order _od=(Order)getHibernateTemplate().get(Order.class, order.getId());
		return _od ;
	}

	public PageBean getOrderList(final String hql,final  PageBean pb) {
		getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				  Query query=session.createQuery(hql);
				  pb.setTotalitem(query.list().size());
				  query.setFirstResult((pb.getPageon()-1)*pb.getPagesize());
				  query.setMaxResults(pb.getPagesize());
				  pb.getTotalpage();
				  pb.setList(query.list());
				return null;
			}
		});
		return pb;
	}

	public void saveOrder(Order order) {

		getHibernateTemplate().save(order);
	}

	public void updateOrder(Order order) {

		getHibernateTemplate().update(order);
	}

}
